package com.eqdd.yiqidian.ui

import android.app.Activity
import android.content.Intent
import android.support.v7.widget.RecyclerView
import android.view.View
import com.eqdd.yiqidian.R
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector
import com.eqdd.yiqidian.base.Config
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditSelectBean
import com.eqdd.yiqidian.ui.base.SlimBaseMultiEditActivity
import kotlinx.android.synthetic.main.activity_recyclerview.*

/**
 * Created by jhj on 18-5-29.
 */
class MultiSelectListActivity : SlimBaseMultiEditActivity() {

    override fun initBinding() {
        clearData()
        setContentView(R.layout.activity_recyclerview)
        val title = intent.getStringExtra(Config.TITLE)
        initTopTitleBar(View.VISIBLE, title)
        initTopRightText("确定") {
            val builder = StringBuilder()
            (slimAdapterEx.data as List<SlimEditSelectBean>).filter { it.isChecked }.forEach { builder.append(it.title).append(",") }
            val intent = Intent()
            intent.putExtra("data", builder.substring(0, builder.length - 1))
            setResult(Activity.RESULT_OK, intent)
            finish()
        }

    }

    override fun initRecyclerView(): RecyclerView {
        return recyclerView
    }

    override fun itemClick(p: Int) {
        (slimAdapterEx.getDataItem(p) as SlimEditSelectBean).isChecked = !(slimAdapterEx.getDataItem(p) as SlimEditSelectBean).isChecked
        slimAdapterEx.notifyItemChanged(p)
    }

    override fun setEditBeans() {
        val select = intent.getStringExtra("select")
        val dataList = intent.getSerializableExtra("data") as Array<String>?
        val array = select?.split(",")
        dataList?.forEachIndexed { index, s ->
            if (array?.contains(s) == true) {
                datas.add(SlimEditSelectBean(s, index, true))
            } else {
                datas.add(SlimEditSelectBean(s, index, false))
            }
        }

        slimAdapterEx
                .register(R.layout.list_item_selected, SlimInjector<SlimEditSelectBean> { data, injector ->
                    injector?.text(R.id.tv_title, data?.title)
                            ?.image(R.id.checkbox, if (data?.isChecked == true) R.drawable.icon_choice else R.drawable.icon_choice_no)
                })
    }


}